namespace ScrumSprintMonitor.Samples.Feed
{
	#region #using Directives

	using System;
	using System.ComponentModel.Composition;

	using Extensibility;
	using Extensibility.Events;

	using Infrastructure;

	using Microsoft.Practices.Composite.Events;

	#endregion

	[Export(typeof(ScrumSprintMonitorAddIn))]
	[AddIn(Name = "Feed")]
	public class FeedAddIn : ScrumSprintMonitorAddIn
	{
		#region Private Fields

		private FeedBuilderService feedBuilderService;

		#endregion

		#region Overrides of ScrumSprintMonitorAddIn

		public override void Load()
		{
			var eventAggregator = ServiceProvider.GetService<IEventAggregator>();
			eventAggregator.GetEvent<MonitorStartedEvent>().Subscribe(OnMonitorStarted);
		}

		private void OnMonitorStarted(EventArgs obj)
		{
			var reportableData = (IReportableData) ServiceProvider.GetService(typeof(IReportableData));
			var teamMemberInfoProvider = (ITeamMemberInfoProvider) ServiceProvider.GetService(typeof(ITeamMemberInfoProvider));
			var logger = ServiceProvider.GetService<ILogger<FeedBuilderService>>();

			this.feedBuilderService = new FeedBuilderService(ServiceProvider.GetService<IAddInHost<FeedAddIn>>(), reportableData, teamMemberInfoProvider,
			                                                 logger, ServiceProvider);
		}

		public override void Unload()
		{
			if (this.feedBuilderService != null)
			{
				this.feedBuilderService.Dispose();
				this.feedBuilderService = null;
			}
		}

		#endregion
	}
}